package org.idea.spring.aop.cglib.proxy;


import org.idea.spring.aop.cglib.MyBis;
import org.idea.spring.aop.cglib.fastclass.MyBisFastClass;

import java.lang.reflect.Method;
import java.util.HashMap;

/**
 * @Author linhao
 * @Date created in 4:55 下午 2021/5/6
 */
public class MyEnhancer {

    public Object source;

    public MyMethodInterceptor myMethodInterceptor;

    public MyMethodProxy myMethodProxy;

    public void setMyMethodInterceptor(MyMethodInterceptor myMethodInterceptor) {
        this.myMethodInterceptor = myMethodInterceptor;
    }

    public void setMyMethodProxy(MyMethodProxy myMethodProxy) {
        this.myMethodProxy = myMethodProxy;
    }

    public void setSource(Object source) {
        this.source = source;
    }

    public MyBis create() {
        if (!MyBis.class.equals(this.source.getClass())) {
            return null;
        }
        this.setMyMethodProxy(new MyBisFastClass());
        Method[] methods = MyBis.class.getClass().getMethods();
        HashMap<String, Method> methodHashMap = new HashMap<>(methods.length);
        for (Method method : methods) {
            methodHashMap.put(method.getName(), method);
        }
        MyBisEnhancerClass$CGLIB myBis = new MyBisEnhancerClass$CGLIB();
        myBis.setSource(this.source);
        myBis.setMyMethodProxy(this.myMethodProxy);
        myBis.setMyMethodInterceptor(myMethodInterceptor);
        myBis.setMethodMap(methodHashMap);
        return myBis;
    }
}
